package com.shop.cloud.controller.hongfu;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.RebateHistory;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.IRebateHistoryService;
import com.shop.cloud.util.Page;

import io.swagger.annotations.ApiOperation;

/**
 * 返利管理
 * @author Yan Weiwei
 *
 */
@RestController	
@RequestMapping(value = "api")
public class RebateManageController extends BaseController {
	
	@Autowired
	private IRebateHistoryService rebateHistoryService;

	@ApiOperation(value = "获取用户某个年月的返利账单", notes = "获取用户某个年月的返利账单")
	@RequestMapping(value = "/getUserRebateList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<RebateHistory>> getUserRebateList(@CurrentUserId Long userId, @RequestBody RebateHistory rebateHistory){
		
		if(null == rebateHistory.getThisYear()){
			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			rebateHistory.setThisYear(year);
		}
		if(null == rebateHistory.getThisMonth()){
			Calendar calendar = Calendar.getInstance();
			int month = calendar.get(Calendar.MONTH) + 1;
			rebateHistory.setThisMonth(month);
		}
		rebateHistory.setUserId(userId);
		rebateHistory.setOrderBy("REBATE_HISTORY.create_time DESC");
		Page<RebateHistory> data = rebateHistoryService.page(rebateHistory);
		
		List<RebateHistory> list = data.getData();
		if(null != list && list.size() > 0){
			float totalAmount = 0f;
			int thisYear = 0;
			int thisMonth = 0;
			for(RebateHistory insData : list){
				float amount = insData.getAmount();
				totalAmount = totalAmount + amount;
				
				thisYear = insData.getThisYear();
				thisMonth = insData.getThisMonth();
			}
			list.get(0).setShowTotalAmount(totalAmount);
			list.get(0).setShowYearAndMonth(thisYear+"年"+thisMonth+"月");
		}
		data.setData(list);
		
		return new ObjectDataResponse<Page<RebateHistory>>(data);
	}
	
	@ApiOperation(value = "获取账单年列表", notes = "获取账单年列表")
	@RequestMapping(value = "/getRebateYear", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<RebateHistory> getRebateYear(@CurrentUserId Long userId){
		
		RebateHistory data = rebateHistoryService.handleRebateYearAndMonth(userId);
		
		return new ObjectDataResponse<RebateHistory>(data);
	}
}
